package com.cdcas;

import java.util.Random;
import java.util.Scanner;

public class MathApp2 {

        private static final int MAP_SIZE = 20;
        private static final double PI = Math.PI;
        private static int fuel = 100;
        private static double[] shipPos = {5.0, 5.0};
        private static double[][] obstacles = new double[5][2];
        private static Random rand = new Random();

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            generateObstacles();

            System.out.println("🚀 欢迎来到宇宙导航员！");
            System.out.println("▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂");
            System.out.println("游戏目标：抵达地图右侧边缘");
            System.out.println("操作指南：");
            System.out.println("- 输入移动角度(0-360度)和力量(1-10)");
            System.out.println("- 避开红色★小行星和🌀黑洞");
            System.out.println("- 绿色※星云可补充燃料");
            System.out.println("- 剩余燃料：" + fuel + "单位\n");

            while (fuel > 0 && shipPos[0] < MAP_SIZE-1) {
                printMap();
                System.out.print("\n请输入角度和力量（例：45 8）→ ");

                try {
                    double angle = sc.nextDouble();
                    double power = Math.min(sc.nextDouble(), 10);
                    fuel -= (int)power;

                    moveShip(Math.toRadians(angle), power/2);
                    checkEvents();
                } catch (Exception e) {
                    System.out.println("⚠ 无效输入！请使用数字格式");
                    sc.nextLine();
                }
            }

            System.out.println("\n▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂");
            System.out.println(fuel <= 0 ? "💀 燃料耗尽！任务失败..." : "🎉 成功抵达！星际领航员成就达成！");
            System.out.println("最终位置：" + String.format("[%.1f, %.1f]", shipPos[0], shipPos[1]));
            sc.close();
        }

        private static void moveShip(double radians, double power) {
            shipPos[0] += power * Math.cos(radians);
            shipPos[1] += power * Math.sin(radians);
            shipPos[0] = Math.min(Math.max(shipPos[0], 0), MAP_SIZE-1);
            shipPos[1] = Math.min(Math.max(shipPos[1], 0), MAP_SIZE-1);
        }

        private static void generateObstacles() {
            for (int i=0; i<obstacles.length; i++) {
                obstacles[i][0] = rand.nextInt(MAP_SIZE-5)+5;
                obstacles[i][1] = rand.nextInt(MAP_SIZE);
            }
        }

        private static void checkEvents() {
            // 检查小行星碰撞
            for (double[] obj : obstacles) {
                double dist = Math.hypot(shipPos[0]-obj[0], shipPos[1]-obj[1]);
                if (dist < 2.0) {
                    System.out.println("💥 小行星撞击！损失20单位燃料");
                    fuel -= 20;
                }
            }

            // 随机事件
            if (Math.random() < 0.3) {
                if (rand.nextBoolean()) {
                    System.out.println("🌀 遭遇黑洞！位置偏移");
                    shipPos[0] += Math.random()*4-2;
                    shipPos[1] += Math.random()*4-2;
                } else {
                    System.out.println("✨ 穿越星云！补充15燃料");
                    fuel += 15;
                }
            }
        }

        private static void printMap() {
            System.out.println();
            for (int y=0; y<MAP_SIZE; y++) {
                for (int x=0; x<MAP_SIZE; x++) {
                    char c = '·';
                    // 绘制飞船
                    if (Math.hypot(x-shipPos[0], y-shipPos[1]) < 0.5) {
                        c = '✈';
                    }
                    // 绘制障碍物
                    for (double[] obj : obstacles) {
                        if (Math.round(obj[0]) == x && Math.round(obj[1]) == y) {
                            c = (Math.random() > 0.2) ? '★' : '※';
                        }
                    }
                    // 绘制右侧边界
                    if (x == MAP_SIZE-1) c = '‖';
                    System.out.print((x == 0 || x == MAP_SIZE-1) ? "\033[34m" + c + "\033[0m" : c);
                }
                System.out.println();
            }
            System.out.println("剩余燃料：" + Math.max(fuel,0) + " 当前位置：" +
                    String.format("[%.1f, %.1f]", shipPos[0], shipPos[1]));
        }
}
